package com.project.lk.nurseapp.bloodglucose.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.project.lk.nurseapp.base.BaseApi;
import com.project.lk.nurseapp.base.PostBaseModel;
import com.project.lk.nurseapp.login.model.HintBean;
import com.project.lk.nurseapp.utils.JsonUtils;
import com.project.lk.nurseapp.utils.ToastUtils;
import com.project.lk.nurseapp.utils.UserInfoUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.FormBody;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class PostGlucoseModel extends PostBaseModel {

    public void postLoadJson(String url, String bloodSugar, String testTesult, String recordTime,String timeType) {
        Random random = new Random();
        int nextInt = random.nextInt();
        FormBody.Builder builder = new FormBody.Builder();
        long currentTimeMillis = System.currentTimeMillis();
        builder.add("signature", postSignToken(currentTimeMillis + "", nextInt + ""))
                .add("timestamp", currentTimeMillis + "")
                .add("nonce", nextInt + "")
                .add("bloodSugar", bloodSugar)
                .add("recordTime", recordTime)
                .add("testTesult", testTesult)
                .add("timeType",timeType)
                .add("accountId", new UserInfoUtils().getAccountId())
                .add("baseUserInfoId", new UserInfoUtils().getBaseUserInfoId());
        getJsonData(url, builder.build());
    }

    @Override
    public List analyzeJson(String json, String url) {
        Log.e("MLOG", url + json);
        try {
            List<HintBean> list = new ArrayList<>();
            HintBean bean = (HintBean) JsonUtils.getJsonUtils()
                    .toObject(new TypeToken<HintBean>() {
                    }.getType(), json);
            if (TextUtils.equals(bean.getMsg(), "success")) {
                postSuccess();
            } else {
                postFail();
            }
            ToastUtils.showShortToast(bean.getInfo());//显示提示信息
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.showShortToast("测量数据上传失败");
//            codeFail();
        }
        return null;
    }

    @Override
    public void postSuccessOnMainThread(List list, String url) {

    }

    @Override
    public void postFailedOnMainThread(String url) {
        postFail();
    }

    //登录成功
    public void postSuccess() {
        bridge.bridge(BaseApi.post_glucose_success, null);

    }

    //登录失败
    public void postFail() {
        bridge.bridge(BaseApi.post_glucose_fail, null);
    }
}
